/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.generators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bugstat.common.TimeInterval;
import org.bugstat.common.bugs.Bug;
import org.bugstat.common.reports.DateKey;
import org.bugstat.common.reports.IntegerValue;
import org.bugstat.common.reports.Report;
import org.bugstat.common.reports.ReportKey;
import org.bugstat.common.reports.ReportType;
import org.bugstat.common.reports.ReportValue;
import org.bugstat.common.reports.ReportValueName;
import org.bugstat.common.reports.StringValueName;
import org.bugstat.common.utils.Comparators;
import org.bugstat.generators.reports.ReportGeneratorBase;

public class DummyReportGenerator
    extends ReportGeneratorBase
{
    private DummyReport currentReport = new DummyReport();
    private Date           currentDate   = null;
    

    public String getName()
    {
        return "Dummy Generator";
    }
    
    @Override
    public void startedGeneratingReports( TimeInterval timeInterval, ReportGeneratorContext context )
    {
        currentReport.clear();        
    }
    
    @Override
    public void periodStarted(Date endDate)
    {
        currentDate = endDate;
    }

    @Override
    public void processBug(Bug bug)
    {
        currentReport.incrementValue( currentDate );
    }

    @Override
    public List<Report> getGeneratedReports()
    {
        return Arrays.asList( (Report)currentReport );
    }
    
   public class DummyReport
        implements Report
    {
        protected Map<Date, Integer> values = new HashMap<Date, Integer>();
        
    
        @Override
        public ReportValue getValueFor(ReportKey key)
        {
            if ( key instanceof DateKey )
            {
                Integer value = values.get(((DateKey)key).getDate());
                return new IntegerValue( value );
            }
            else
            {
                throw new IllegalArgumentException("Specified key is not DateKey: " + key );
            }
        }
    
        @Override
        public List<? extends ReportKey> getKeys()
        {
            final List<DateKey> result = new ArrayList<DateKey>();
            for ( Date date : values.keySet() )
            {
                result.add( new DateKey( date ) );
            }
            Collections.sort( result, Comparators.dateKeyComparator );
            
            return result;
        }
    
    
        @Override
        public String getGroup()
        {
            return "Dummy";
        }
    
        @Override
        public String getKeyName()
        {
            return "Dummy date";
        }
    
        @Override
        public ReportValueName getValueName()
        {
            return new StringValueName( "Dummy value" );
        }
        
        @Override
        public String getName()
        {
            return "Dummy report";
        }

        @Override
        public ReportType getType()
        {
            return ReportType.GRAPH;
        }

        public void putValue( final Date date, final Integer value )
        {
            values.put( date, value );
        }
        
        public void incrementValue( final Date date )
        {
            Integer currentValue = values.get( date );
            if ( currentValue == null )
            {
                currentValue = 0;
            }
            
            putValue( date, currentValue + 1 );
        }
        
        public void decrementValue( final Date date )
        {
            Integer currentValue = values.get( date );
            if ( currentValue == null )
            {
                currentValue = 0;
            }
            
            putValue( date, currentValue - 1 );
        }
        
        public void clear()
        {
            values.clear();
        }

		@Override
		public boolean isVisibleByDefault() 
		{
			return false;
		}
    
    }



}
